package edu.miami.group.photolog;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.MediaStore;
import android.util.Log;
//=============================================================================
public class PhlogBase {

	//-----------------------------------------------------------------------------

	public static final String DATABASE_NAME = "photoLog.db";
	private static final int DATABASE_VERSION = 1;
	private static String PICTURE_TABLE_NAME;
	private static  String CREATE_PICTURE_TABLE;
	

	private DatabaseHelper dbHelper;
	private SQLiteDatabase theDB;
	//-----------------------------------------------------------------------------
	public PhlogBase(Context theContext, String tableName) {

		PICTURE_TABLE_NAME = tableName;
		CREATE_PICTURE_TABLE = "CREATE TABLE IF NOT EXISTS "
				+ PICTURE_TABLE_NAME + "(_id INTEGER PRIMARY KEY AUTOINCREMENT, title " +
				"TEXT, description TEXT, date INTEGER NOT NULL, picVid BLOB, longitude REAL, latitude REAL);";
		dbHelper = new DatabaseHelper(theContext);
		theDB = dbHelper.getWritableDatabase();
		dbHelper.onCreate(theDB);
	}
	//-----------------------------------------------------------------------------
	//Method that goes into the database and will retrieve the contents of a cursor and put it into a contents value object
	public ContentValues cursorData (Cursor location){
		int index = 0;
		if (location != null && location.moveToFirst()){
			
			ContentValues contentObject = new ContentValues();
			String [] fieldNames = location.getColumnNames();

			for(index = 0; index < fieldNames.length; index++){
				
				if (fieldNames[index].equals("_id")){
					contentObject.put("_id",Integer.valueOf(location.getInt(index)));
				} else if (fieldNames[index].equals("title")) {
					contentObject.put("title",location.getString(index));
				}else if (fieldNames[index].equals("description")){
					contentObject.put("description", location.getString(index));
				}else if (fieldNames[index].equals("date")){
					contentObject.put("date",Long.valueOf(location.getLong(index)));
				}else if (fieldNames[index].equals("picVid")){
					//check this line
					contentObject.put("picVid",location.getBlob(index) );
				}else if (fieldNames[index].equals("latitude")){
					//used a double for precision purposes 
					contentObject.put("latitude", Double.valueOf(location.getDouble(index)));
				}else if (fieldNames[index].equals("longitude")){
					//used a double for precision purposes
					contentObject.put("longitude", Double.valueOf(location.getDouble(index)));
				}

			}
			return contentObject;
		}
		return null;
	}

	//----------------------------------------------------------------------------------------------------
	//Adds a image to the database
	public boolean addPost(ContentValues postData) {
		
		return(theDB.insert(PICTURE_TABLE_NAME,null,postData) >= 0);

	}
	//--------------------------------------------------------------------------------------------------
	//closes the database
	public void close() {

		dbHelper.close();
		theDB.close();
	}
	//-------------------------------------------------------------------------------------------------------
	//updates the database
	public boolean updatePost(long genericID, ContentValues contentValuesObject)
	{
		return theDB.update(PICTURE_TABLE_NAME, contentValuesObject, "_id =" + genericID, null) > 0;
	}
	//-----------------------------------------------------------------------------


	//-----------------------------------------------------------------------------
	//deletes a post in the database
	public boolean deletePost(long postID) {

		return(theDB.delete(PICTURE_TABLE_NAME,"_id =" + postID,null) > 0);
	}
	//-----------------------------------------------------------------------------
	//fetches images  in the database
	public Cursor fetchAllPosts() {

		String[] fieldNames = {"_id","title","description", "date","picVid","longitude", "latitude"};
		//changed the ordering to _id
		return(theDB.query(PICTURE_TABLE_NAME,fieldNames,null,null,	null,null,"_id"));
	}

	
	public Cursor fetchAllPosts(String sortName) {

		String[] fieldNames = {"_id","title","description", "date","picVid","longitude", "latitude"};
		
		if (!sortName.equals("_id") && !sortName.equals("date") && !sortName.equals("title")){
			throw new IllegalArgumentException();
		}
		return(theDB.query(PICTURE_TABLE_NAME,fieldNames,null,null,	null,null,sortName));
	}

	//-------------------------------------------------------------------------------
	public ContentValues getDataByID(long genericID){
		
		Cursor cursor = theDB.query(PICTURE_TABLE_NAME, null, "_id = "+ genericID, null, null, null, null);
		cursor.moveToFirst();
		ContentValues returnObject = cursorData(cursor);
		cursor.close();
		return returnObject;

	}

	//=============================================================================
	private static class DatabaseHelper extends SQLiteOpenHelper {

		//-------------------------------------------------------------------------
		public DatabaseHelper(Context context) {

			super(context,DATABASE_NAME,null,DATABASE_VERSION);

		}
		//-------------------------------------------------------------------------
		public void onCreate(SQLiteDatabase db) {

			db.execSQL(CREATE_PICTURE_TABLE);
		}
		//-------------------------------------------------------------------------
		@Override
		public void onOpen(SQLiteDatabase db) {
			super.onOpen(db);

		}
		//-------------------------------------------------------------------------
		public void onUpgrade(SQLiteDatabase db,int oldVersion,
				int newVersion) {

			db.execSQL("DROP TABLE IF EXISTS "+PICTURE_TABLE_NAME);
			onCreate(db);
		}
		//-------------------------------------------------------------------------
	}
	//=============================================================================
}
//=============================================================================
